package com.redrose.audioandvideo.camera;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.redrose.audioandvideo.R;
import com.redrose.audioandvideo.bean.Photo;
import com.redrose.audioandvideo.databinding.ActivityPreviewBinding;
import com.redrose.audioandvideo.pagetransformer.RotateDownPageTransformer;
import com.redrose.audioandvideo.ui.activitys.BaseActivity;
import com.redrose.audioandvideo.utils.GlideUtil;
import com.redrose.audioandvideo.utils.LogUtils;
import com.redrose.audioandvideo.widget.ResizableImageView;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/activity/previewactivity")
public class PreviewActivity extends BaseActivity<ActivityPreviewBinding> {
    @Autowired(name="mPhotoList")
    public ArrayList<Photo> mPhotoList;
    private int mCurrentMax;
    private ImagePagerAdapter mAdapter;
    private int mPosition;
    @Override
    public void initData() {
//        mPhotoList = (List<Photo>) getIntent().getSerializableExtra("mPhotoList");
        if (mPhotoList == null) {
            return;
        }
        LogUtils.d(TAG," mPhotoList size== " + mPhotoList.size());
        mCurrentMax = mPhotoList.size();
        initAdapter();
    }

    @Override
    public int setLayout() {
        return R.layout.activity_preview;
    }
    private void initAdapter() {
        mAdapter = new ImagePagerAdapter();
        mBinding.vpPreCurrent.setAdapter(mAdapter);
        /** 翻页动画设置 */
        /*mViewPager.setPageTransformer(false,new ZoomOutPageTransformer());*/
        /*mViewPager.setPageTransformer(false,new DepthPageTransformer());*/
        mBinding.vpPreCurrent.setPageTransformer(false, new RotateDownPageTransformer());
        mBinding.vpPreCurrent.setCurrentItem(mPosition);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return mCurrentMax;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.image_viewpager_item, null);
            ResizableImageView imageView = view.findViewById(R.id.resize_image);
            GlideUtil.loadImage(mContext, mPhotoList.get(position).getPath(), imageView);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
